﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SouslikPod.CommandManager;
using SKYPE4COMLib;
using SouslikPod.Wrappers;

namespace SouslikPod.GUI
{
    public partial class MainForm : Form
    {
        private readonly SkypeWrapper _skypeWrapper;
        readonly List<BaseCommand> _commands;
        
        public MainForm()
        {
            InitializeComponent();

            _skypeWrapper = SkypeWrapper.GetSkype();
            foreach (TChatType chatType in Enum.GetValues(typeof(TChatType)))
            {
                cmbChatTypes.Items.Add(new ListItem(Convert.ToInt32(chatType).ToString(), chatType.ToString()));
            }

            CommandLoader commandLoader = new CommandLoader();
            _commands = commandLoader.LoadCommands();

            _skypeWrapper.AttachToMessageEvent(MessageStatus);
        }


        public void MessageStatus(IChatMessage message, TChatMessageStatus status)
        {
            if (lstChatList.SelectedItem != null && status == TChatMessageStatus.cmsReceived)
            {
                ListItem selectedItem = (ListItem) lstChatList.SelectedItem;
                if(message.Chat.Name.Equals(selectedItem.ID))
                {
                    foreach (BaseCommand command in _commands)
                    {
                        string result = command.Execute(message);
                        if(result != null)
                        {
                            message.Chat.SendMessage(result);
                        }
                    }

                }
            }
        }

        private void lstChatList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstChatList.SelectedItem != null)
            {
                ListItem selectedItem = (ListItem)lstChatList.SelectedItem;
                Chat selectedChat = _skypeWrapper.GetChat(selectedItem.ID);

                txtState.Text = String.Format("Название чата: {0}", selectedChat.FriendlyName);
                
            }
            else
            {
                txtState.Text = "Чат не выбран.";
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (lstChatList.SelectedItem != null)
            {
                ListItem selectedItem = (ListItem)lstChatList.SelectedItem;
                Chat selectedChat = _skypeWrapper.GetChat(selectedItem.ID);
                
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void cmbChatTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstChatList.Items.Clear();
            if (cmbChatTypes.SelectedItem != null)
            {
                ChatCollection chats = _skypeWrapper.GetChats();
                lstChatList.Items.Clear();

                foreach (Chat chat in chats)
                {
                    ListItem selectedItem = (ListItem) cmbChatTypes.SelectedItem;
                    if (chat.Type == (TChatType)Enum.Parse(typeof(TChatType), selectedItem.ID))
                        lstChatList.Items.Add(new ListItem(chat.Name, chat.FriendlyName));

                }
            }

        }



    }
}
